#include <unistd.h>
#include <X11/Xlib.h>

#include "modules/status.h"
#include "modules/clock.h"
#include "modules/cpu.h"
#include "modules/ram.h"
#include "modules/network.h"
#include "modules/batteries.h"

static Layout
layouts[] = {
    // FORMAT             SIZE  TYPE              FUNCTION
    {" down: %0.1LfkB/s ",  32, type_long_double, {.Lf = get_down_kbps}},
    {" up: %0.1LfkB/s ",    32, type_long_double, {.Lf = get_up_kbps}},
    {" cpu: %0.1Lf%% ",      8, type_long_double, {.Lf = get_cpu}},
    {" ram: %0.1Lf%% ",      8, type_long_double, {.Lf = get_ram}},
    {" battery: %d%% ",     16, type_int,         {.d  = read_batteries}},
    {" time: %02d:",        16, type_int,         {.d  = get_hour}},
    {"%02d ",                8, type_int,         {.d  = get_minute}},
};

void
set_status(char * str)
{
    Display * dpy;

    if (!(dpy = XOpenDisplay(NULL))) {
        fprintf(stderr, "status: cannot open display.\n");
        return;
    }

    XStoreName(dpy, DefaultRootWindow(dpy), str);
    XSync(dpy, False);

    XCloseDisplay(dpy);
}

void
update_arg(Arg * arg)
{
    arg->last_time       = get_time(NULL);
    arg->last_down_bytes = read_down_bytes(NULL);
    arg->last_up_bytes   = read_up_bytes(NULL);
    long * jiffies = get_jiffies(NULL);
    arg->last_jiffies[0] = (long *) jiffies[0];
    arg->last_jiffies[1] = (long *) jiffies[1];

    sleep(1);
}
